<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav main-header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            送检单查询
        </div>
    </header>
    <div class="aui-bar aui-bar-nav sub-header"></div>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;padding-bottom: 50px;">
        <div class="white_background">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">发货单号</div>
                        <input type="text" class="aui-input" placeholder="请扫描/输入发运单号" style="width: 80%;"
                            id="consignment" onclick="getIdSelector();" onfocus="focusFn(this)">

                    </div>
                </li>
            </ul>

            <div style="width:100%;margin-top:1%;"></div>

            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">箱码</div>
                        <input type="text" class="aui-input" placeholder="请扫描原箱码" style="width: 80%;" id="tm"
                            onclick="getIdSelector();" onfocus="focusFn(this)">

                    </div>
                </li>
            </ul>

            <div class="aui-btn aui-btn-info aui-btn-block" onclick="getInspectionInfo()">查询</div>

            <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:2%;"
                id="tbodyHtml">

            </div>
            <div class="aui-content aui-margin-b-15">
                <ul class="aui-list aui-media-list" id="mrlList">

                </ul>
            </div>
        </div>
    </div>

    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>


</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    var idSelector = "";
    // var consignment = ""; //发货单号
    // var tm = ""; //箱号
    var scanRule = ""; //箱码解析规则
    var bindList = "";//物料数据列表

    //初始化查询仓库下拉框
    apiready = function () {

        // $("#consignment").focus();
        api.setFocus({
            inputId: 'consignment',
            focus: true
        })
        idSelector = 'consignment'
        //查询箱码解析规则
        initScanRule();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //alert(ret);扫码的结果都是编码
            //1：发货单号
            if (idSelector == 'consignment') {

                $("#consignment").val(ret);

                getInspectionInfo();
                //2:箱码
            } else if (idSelector == 'tm') {

                $("#tm").val(ret);
                //箱码解析
                tmSplit(ret);
                getInspectionInfo();
            }
        });
    }

    //绑定
    function save() {
        if (!clickThrottle()) {
            return;
        }
        var trayyinfoid = $("#trayyinfoid").val();
        var trayyinfotargetid = $("#trayyinfotargetid").val();

        //原容器
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            api.toast({
                msg: '请扫描原容器！',
                duration: 3000
            });
            return;
        }

        //目标容器
        if (trayyinfotargetid == "" || trayyinfotargetid == null || trayyinfotargetid == undefined) {
            api.toast({
                msg: '请扫描目标容器！',
                duration: 3000
            });
            return;
        }

        console.log('bindList:' + JSON.stringify(bindList));

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/commitContainerMrl";
        var data = {
            values: {
                oriContainerTm: trayyinfoid, //原容器条码
                targetContainerTm: trayyinfotargetid, //目标容器条码
                transferMrlList: bindList,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {

                    api.toast({
                        msg: "提交成功",
                        duration: 3000
                    });

                    //光标回到目标容器条码
                    $("#trayyinfotargetid").focus();
                    $("#trayyinfotargetid").select();
                    idSelector = "trayyinfotargetid";
                } else {

                    $("#trayyinfotargetid").val("");
                    $("#trayyinfotargetid").select();
                    idSelector = "trayyinfotargetid";
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {

                $("#trayyinfotargetid").val("");
                $("#trayyinfotargetid").select();
                idSelector = "trayyinfotargetid";
                api.hideProgress();
                // alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //自动保存
    function saveAuto() {
        var trayyinfoid = $("#trayyinfoid").val();
        var tm = $("#tm").val();
        /* var supplierCode = $("#supplierCode").val();
        var mrlCode = $("#mrlCode").val();
        var lotCode = $("#lotCode").val();
        var qty = $("#qty").val();
        var boxnum = $("#boxnum").val(); */

        //容器
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            return;
        }

        //箱码
        if (tm == "" || tm == null || tm == undefined) {
            return;
        }
        //物料号非空校验
        // if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
        //     return;
        // }
        // //批次号非空校验
        // if (lotCode == "" || lotCode == null || lotCode == undefined) {
        //     return;
        // }

        // //数量非空校验
        // if (qty == "" || qty == null || qty == undefined || parseFloat(qty) <= 0) {
        //     return;
        // }

        // //箱号非空校验
        // if (boxnum == "" || boxnum == null || boxnum == undefined) {
        //     return;
        // }

        save();
    }

    //发货单号监听回车事件
    $("#consignment").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                console.log(tm);
                $("#consignment").val(tm);
                $("#consignment").focus();
                $("#consignment").select();
                getInspectionInfo();
            }

        }
    });
    //箱号监听回车事件
    $("#tm").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                console.log(tm);
                $("#tm").val(tm);
                $("#tm").focus();
                $("#tm").select();
                getInspectionInfo();
            }

        }
    });

    //扫码查询返回送检单信息
    function getInspectionInfo() {




        var arr = $("#tm").val().split(",");
        if (arr.length == 10) {
            var boxnum = arr[9];
        } else {
            var boxnum = $("#tm").val();
        }
        var consignment = $("#consignment").val();

        if ((consignment == "" || consignment == null || consignment == undefined) && (arr == "" || arr == null || arr == undefined)) {
            api.toast({
                msg: "请输入查询条件！",
                duration: 3000
            });
            return;
        }




        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchInspectionInfo";
        var data = {
            values: {
                consignmentQ: consignment,
                boxnumQ: boxnum,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);

                    console.log('list:' + JSON.stringify(list));

                    bindList = list;
                    var html = "";
                    var bindQtyID = "";
                    if (list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            bindQtyID = "bindQty_" + i
                            html += "<div class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 220px;padding: 0.2rem 0.2rem;font-size: 15px;border-radius: 8px;' onclick='goPage(\"" + list[i].consignment + "," + list[i].inspection + "," + list[i].mrlcode + "," + list[i].drawing + "," + list[i].lotcode + "\");'>";
                            html += "<table style='width:100%;'>";
                            html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>送检单号：" + list[i].inspection + "</td></tr>";
                            html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>物料号：" + list[i].mrlcode + "</td></tr>";
                            html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>物料名：" + list[i].mrlname + "</td></tr>";
                            html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>规格：" + list[i].drawing + "</td></tr>";
                            html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>批次：" + list[i].lotcode + "</td></tr>";
                            html += "<tr style='width:100%;'><td style='width:50%;'>总箱数：" + list[i].boxnum + "</td><td style='width:50%;'>物料总数：" + list[i].mrlqty + "</td></tr> ";
                            html += "<tr style='width:100%;'><td style='width:50%;'>已退箱数：" + list[i].rejectboxnum + "</td><td style='width:50%;'>物料总数：" + list[i].rejectmrlqty + "</td></tr> ";
                            html += "</table></div>";
                        }
                    } else {
                        html += "<div class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 180px;padding: 0.2rem 0.2rem;font-size: 15px;border-radius: 8px;'>";
                        html += "<table style='width:100%;'>";
                        html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>送检单号：</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>物料号：</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>物料名：</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>规格：</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:100%;' colspan='2'>批次：</td></tr>";
                        html += "<tr style='width:100%;'><td style='width:50%;'>总箱数：</td><td style='width:50%;'>物料总数：<input style='width:40%;' ></input></td></tr>";
                        html += "<tr style='width:100%;'><td style='width:50%;'>已退箱数：</td><td style='width:50%;'>物料总数：<input style='width:40%;' ></input></td></tr>";
                        html += "</table></div>";
                    }

                    $api.html($api.byId("tbodyHtml"), html);

                    // 光标所在位置
                    // $("#traynum").focus();
                    // idSelector = "traynum";

                    /*  var html = "";
                     for (var i = 0; i < list.length; i++) {
                         html += "<tr style='text-align: center;color: black;font-size: 10px;background-color: #F5F5F5;'>";
                         html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                         html += "<td name='mrlName'>" + list[i].mrlName + "</td>";
                         html += "<td name='ext05'>" + list[i].ext05 + "</td>";
                         html += "<td name='qty'>" + list[i].qty + "</td>";
                         html += "</tr>";
                     }
                     $api.html($api.byId("tbodyHtml"), html); */

                    // 两种方式 都试试
                    // var evaluation = doT.template($api.dom('#chkBillTemp').innerHTML);
                    // $api.dom('#mrlList').innerHTML = evaluation(list);

                    //失败
                } else if (myobj.code == "1") {
                    $("#consignment").val("");
                    $("#consignment").select();
                    idSelector = "consignment";

                    //清空表格数据
                    // $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#consignment").val("");
                $("#consignment").select();
                idSelector = "consignment";
                api.hideProgress();
                // alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //扫描之后箱码解析
    function tmSplit(sm) {
        $("#tm").val(sm);
        //直接解析
        if (scanRule == "old") {

            //本地解析
            localAnalyze(sm);

        } else {

            //后台解析
            postAnalyze(sm);
        }
    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        $("#supplierCode").val(arr[0]);
        $("#mrlCode").val(arr[1]);
        $("#qty").val(arr[2]);
        $("#lotCode").val(arr[3]);
        $("#tmID").val(arr[4]);
        $("#boxnum").val(arr[9]);
        //箱号选中
        $("#tm").select();

        //自动保存
        saveAuto();
    }

    //后台解析箱码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCodeCommon";
        var data = {
            values: {
                tm: sm, //箱码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).val(map[key]);
                    }

                    //自动保存
                    saveAuto();
                } else if (myobj.code == '1') {

                    $("#tm").val("");
                    $("#tm").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                // alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
    function focusFn(ele) {
        $api.attr(ele, 'readonly', 'readonly');
        setTimeout(() => {
            $api.removeAttr(ele, 'readonly');
        }, 200);
    };

    function goPage(str) {
        var arr = str.split(",");
        api.openWin({
            name: 'uwmInspectionRejectBox',
            url: 'uwmInspectionRejectBox.html',
            pageParam: {
                name: "uwmInspectionReject",
                consignment: arr[0],
                inspection: arr[1],
                mrlcode: arr[2],
                drawing: arr[3],
                lotcode: arr[4]
            }

        });
    }

</script>

</html>